package flc.ast.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.bean.MyRecordBean;
import flc.ast.databinding.ActivityRecognizePlantBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.apis.ApiManager;
import stark.common.apis.base.ImgCarRet;
import stark.common.apis.base.ImgPlantRet;
import stark.common.base.IReqRetCallback;
import stark.common.basic.utils.UriUtil;

public class RecognizePlantActivity extends BaseAc<ActivityRecognizePlantBinding> {

    public static String imgPath;

    @Override
    protected int onCreate() {
        return R.layout.activity_recognize_plant;
    }

    @Override
    protected void initView() {
        mDataBinding.ivRecognizeBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.ivRecognizePlant.setOnClickListener(this);
        mDataBinding.ivRecognizeCopy.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.ivRecognizePlant:
                Uri uri= UriUtil.path2Uri(mContext,imgPath);
                identifyPlant(uri);
                break;
            case R.id.ivRecognizeCopy:
                copy();
                break;
        }
    }

    @Override
    protected void initData() {
        Glide.with(mContext).load(imgPath).into(mDataBinding.ivRecognizePlantImg);
        Glide.with(mContext).load(imgPath).into(mDataBinding.ivRecognizePlantImgComp);
    }

    private void identifyPlant(Uri uri) {
        mDataBinding.llRecognizeTip.setVisibility(View.VISIBLE);
        Animation animation = new RotateAnimation(0, 359, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setDuration(1000);
        animation.setRepeatCount(1000);
        animation.setFillAfter(true);//设置为true，动画转化结束后被应用
        mDataBinding.ivRecognizeTip.startAnimation(animation);//开始动画
        ApiManager.imgApi().identifyPlant(this, uri, new IReqRetCallback<List<ImgPlantRet>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<ImgPlantRet> result) {
                mDataBinding.ivRecognizeTip.clearAnimation();//停止动画
                mDataBinding.llRecognizeTip.setVisibility(View.GONE);
                if (result != null) {
                    try {
                        JSONArray jsonArray=new JSONArray(GsonUtils.toJson(result));
                        JSONObject jsonObject=jsonArray.getJSONObject(0);
                        String name=jsonObject.getString("name");
                        String desc=jsonObject.getJSONObject("baike_info").getString("description");
                        if (desc.equals("null")){
                            desc="";
                        }
                        if (name.equals(getString(R.string.no_plant))){
                            ToastUtils.showShort(getString(R.string.recognize_error));
                            return;
                        }
                        mDataBinding.llRecognizePlant.setVisibility(View.GONE);
                        mDataBinding.llRecognizePlantComp.setVisibility(View.VISIBLE);
                        mDataBinding.ivRecognizeCopy.setVisibility(View.VISIBLE);
                        List<MyRecordBean> listSave = new ArrayList<>();
                        listSave.add(new MyRecordBean(imgPath,name,desc,1));
                        List<MyRecordBean> listSp = AppSpUtils.getRecordList();
                        if (listSp != null && listSp.size() > 0) {
                            listSp.addAll(listSave);
                            AppSpUtils.saveRecordList(listSp);
                        } else {
                            AppSpUtils.saveRecordList(listSave);
                        }
                        mDataBinding.tvRecognizeName.setText(name);
                        mDataBinding.tvRecognizeDesc.setText(desc);
                        if (desc.isEmpty()){
                            mDataBinding.tvRecognizeDesc.setVisibility(View.GONE);
                        }else {
                            mDataBinding.tvRecognizeDesc.setVisibility(View.VISIBLE);
                        }
                    } catch (JSONException e) {
                        throw new RuntimeException(e);
                    }
                }else {
                    ToastUtils.showShort(getString(R.string.recognize_error));
                }
            }
        });
    }

    private void copy(){
        ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);//系统剪切板管理器
        String textToCopy = mDataBinding.tvRecognizeDesc.getText().toString();
        if (textToCopy.isEmpty()) return;
        ClipData clip = ClipData.newPlainText("label", textToCopy);
        clipboard.setPrimaryClip(clip);
        ToastUtils.showShort(getString(R.string.copy_suc));
    }

}